package com.bjhb.wuranyuan.web.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bjhb.wuranyuan.dao.QysbxxMapper;
import com.bjhb.wuranyuan.dao.QyxxMapper;
import com.bjhb.wuranyuan.dao.SbzbMapper;
import com.bjhb.wuranyuan.pojo.Qysbxx;
import com.bjhb.wuranyuan.pojo.Qyxx;
import com.bjhb.wuranyuan.pojo.Sbzb;

@Controller
public class SbzbAction {

	@Resource
	private SbzbMapper sbzbMapper;
	@Resource
	private QysbxxMapper qysbxxMapper;
	@Resource
	private QyxxMapper qyxxMapper;

	@RequestMapping("/sbzb.htm")
	public String list(HttpServletRequest request,
			@RequestParam("sbid") String sbid) {
		Qysbxx qysbxx = qysbxxMapper.selectBySbid(sbid);
		String qydm = qysbxx.getQydm();
		Qyxx qyxx = qyxxMapper.selectByQydm(qydm);
		List<Sbzb> list = sbzbMapper.selectBySbid(sbid);
		request.setAttribute("list", list);
		request.setAttribute("qysbxx", qysbxx);
		request.setAttribute("qyxx", qyxx);
		return "sbzb";
	}

	@RequestMapping("/sbzb/edit.htm")
	public String edit(HttpServletRequest request, String sbid, int sbtddm,
			String newFlg) {
		Sbzb param = new Sbzb();
		param.setSbid(sbid);
		param.setSbtddm(sbtddm);
		if (newFlg == null || "".equals(newFlg)) {
			Sbzb sbzb = sbzbMapper.getZb(param);
			request.setAttribute("sbzb", sbzb);
		}
		request.setAttribute("sbid", sbid);
		return "sbzb/sbzb_edit";

	}

	@RequestMapping("/sbzb/update.htm")
	public String update(HttpServletRequest request, Sbzb sbzb) {
		sbzbMapper.update(sbzb);
		request.setAttribute("flg", "true");
		return "sbzb/sbzb_edit";
	}

	@RequestMapping("/sbzb/add.htm")
	public String add(HttpServletRequest request, Sbzb sbzb) {
		sbzbMapper.insert(sbzb);
		request.setAttribute("flg", "true");
		return "sbzb/sbzb_edit";
	}

	@RequestMapping("/sbzb/delete.htm")
	public void delete(HttpServletResponse resp, String sbid, int[] sbtddms) {
		if (sbtddms != null) {
			for (int dm : sbtddms) {
				Sbzb sbzb = new Sbzb();
				sbzb.setSbid(sbid);
				sbzb.setSbtddm(dm);
				sbzbMapper.delete(sbzb);
			}
		}
		try {
			resp.sendRedirect("../sbzb.htm?sbid=" + sbid);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
